﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainigWorkshop.BLL;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using TrainigWorkshop.Model;
using Presentation.ViewModel;


namespace Presentation
{
    public partial class StudentMainPage : System.Web.UI.Page
    {

        public void Page_Load(object sender, EventArgs e)
        {
          if(!IsPostBack)
          {
              InitStudentInfo();         
          }
        }
   
        private void InitStudentInfo()
        {
           
            StudentLogic studentbll = new StudentLogic();
            List<StudentModel> student = studentbll.GetAllStudentInfo();
            List<StudentCourseDetailView> studentCourseDetailViewList = new List<StudentCourseDetailView>();
            StudentCourseDetailView studentCourseDetailView;
            foreach (StudentModel obj in student)
            {
                studentCourseDetailView = new StudentCourseDetailView();
                studentCourseDetailView.ClassName = obj.grades.ClassName;
                studentCourseDetailView.StudentGender = obj.StudentGender;
                studentCourseDetailView.StudentId = obj.StudentId;
                studentCourseDetailView.StudentName = obj.StudentName;
                studentCourseDetailViewList.Add(studentCourseDetailView);
            }
            gvStudentInfo.DataSource = studentCourseDetailViewList;
            gvStudentInfo.DataBind(); 
        }

        protected void btnaddStudent_Click(object sender, EventArgs e)
        {
            Boolean type = true;
            Response.Redirect("UpdateStudent.aspx?type=" + type);

        }

        protected void gvStudentInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int StudentId = Convert.ToInt32(gvStudentInfo.DataKeys[e.RowIndex].Value);

            DeleteStudentInfo(StudentId);

        }

        private void DeleteStudentInfo(int StudentId)
        {

            StudentLogic studentbll = new StudentLogic();
            studentbll.DeleteStudentInfo(StudentId);
            List<StudentModel> students = studentbll.GetAllStudentInfo();
            List<StudentCourseDetailView> studentCourseDetailViewList = new List<StudentCourseDetailView>();
            StudentCourseDetailView studentCourseDetailView;
            foreach (StudentModel obj in students)
            {
                studentCourseDetailView = new StudentCourseDetailView();
                studentCourseDetailView.ClassName = obj.grades.ClassName;
                studentCourseDetailView.StudentGender = obj.StudentGender;
                studentCourseDetailView.StudentId = obj.StudentId;
                studentCourseDetailView.StudentName = obj.StudentName;
                studentCourseDetailViewList.Add(studentCourseDetailView);
            }
            this.gvStudentInfo.DataSource = studentCourseDetailViewList;
            this.gvStudentInfo.DataKeyNames = new string[] { "StudentId" };
            this.gvStudentInfo.DataBind();
        }

        protected void gvStudentInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string studentId = this.gvStudentInfo.DataKeys[e.NewEditIndex].Value.ToString();
             Boolean type = false;
            Response.Redirect("UpdateStudent.aspx?id=" + studentId + "&type=" + type);
        }
    }
}